package edu.rpi.bt.client;

import java.util.List;

import com.apress.progwt.client.college.ServiceCache;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;

import edu.rpi.bt.client.domain.Comment;
import edu.rpi.bt.client.domain.Destination;

/**
 * 
 * @author Nate
 *
 * The full panel with all details
 */
public class LocationPanelFull extends LocationPanel {
//    private ServiceCache serviceCache;
//    private Destination destination;
    private Panel commentPanel = new Panel();
    private Panel contentPanel = new Panel();
    
    private LocationPanelFull thisLocationPanel = this;
    
    LocationPanelFull( ServiceCache servCache, long locId ){
        serviceCache = servCache;
        destId=locId;
        loadData( locId );
        init();
    }
    LocationPanelFull( ServiceCache servCache, Destination dest){
        this.serviceCache = servCache;
        this.destination = dest;
        
        init();
    }
    
    private void init(){
        makeServiceCalls();
        
   //     MessageBox.alert("user is " + thisUser.getNickname());
        
        
        BorderLayoutData centerData = new BorderLayoutData(RegionPosition.CENTER);  
        centerData.setMargins(3, 0, 3, 3);  
        
        BorderLayoutData southData = new BorderLayoutData(RegionPosition.SOUTH);  
        southData.setMargins(0, 3, 0, 3);  
       
        setLayout(new BorderLayout());
        setBorder(false);
        setHeight(600);
        
       add( contentPanel(), centerData);
       add( buildCommentPanel(), southData);
        
      
    }


    
    
    public  List<Comment> FindComments(){
     //   System.out.println("inside getdests");
    //    MessageBox.alert("inside get comment..count " + comments.size()); 
        return this.comments;
    }
    
    
    private Panel contentPanel(){
        contentPanel.setCls("btDetailContent");
        contentPanel.setBorder(false);
        contentPanel.setHeight(150);
        return contentPanel;
    }
    

    private Panel buildCommentPanel(){
        
        commentPanel.setCls("btComments");
        commentPanel.setBorder(false);
        commentPanel.setAutoScroll(true);
        commentPanel.setHeight(300);
        commentPanel.setPaddings(0, 0, 0, 5);


        ToolbarButton addCommentButton = new ToolbarButton("Click here to Comment on this Location!", new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
                if(  thisUser != null  ){
                    SaveCommentWindow commentWindow = new SaveCommentWindow( serviceCache, destination, thisUser );
                    commentWindow.setLocationPanel(thisLocationPanel);
                    commentWindow.show();
                } else {
                    MessageBox.alert("You must be logged in to add comments! <a href=\"http://localhost:8080/login.html\">Log in now.</a>");
                }
            }
        });
        
        Toolbar toolbar = new Toolbar();
        toolbar.addButton(addCommentButton);
        
        commentPanel.setTopToolbar(toolbar);        
        return commentPanel;
    }

    protected void setupImages(){
        String html =
            "<table class=\"btDetailContentTbl\" width=\"100%\">" +
            "<tr><td ><div class=\"locname\">" + destination.getName() + "</div>" +
            "<div class=\"locdescrip\">" + destination.getDescription() + "</div>" +
            "</td>" +
            "<td class=\"tdImages\">" +  getImageTags( 4 ) + "</td>" + 
            "</tr>" +
            "<tr></tr>" +
            "</table>";
        
        contentPanel.setHtml(html);
    }
    
    protected void setupComments(){
        commentPanel.setHtml( getFormattedComments() );        
    }
    
    private String getFormattedComments(){
        String html="";
        String[][] commentsData = this.getComments();
        String[] users = {
                "Jimbo Jones",
                "Homer Simpson",
                "Milhouse",
                "Apu Nahasapeemapetilon",
                "Hank Hill",
                "Peter Griffen"
        };
        
        for(int i=0; i<commentsData.length; i++){
            html += "<div class=\"loccomments\">" +
                    "<span class=\"comment\">" + commentsData[i][0] + "</span>" +
        //            "<br><br><span class=\"user\">" + commentsData[i][1] + "</span>" +
                    "</div>";
        }
        return html;
    }
    
    
    public void reinit(){
        init();
    }
    
}
